package ${content.items.controller.packageName};
<#assign assign_EntityName = content.items.entity.className>
<#assign tableName = content.content.tableName>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.ModelMap;
import lombok.extern.slf4j.Slf4j;
import ibp.core.web.runtime.LoginManager;
import ibp.core.annotation.LoggerHandlerRequired;
import ibp.core.annotation.LoggerHandlerRequired.OperateType;
import ibp.ioc.controller.core.BaseController;
import ibp.core.model.PageInfo;
import ibp.core.model.PageParam;
import ibp.core.tools.MyBatisPageHelper;
import ibp.core.annotation.Paging;
import ibp.core.model.OprMessage;
import java.util.List;
import ibp.common.utils.DictUtil;
import ibp.common.utils.UUIDUtil;
import ${content.items.service.packageName}.${content.items.service.className};
import ${content.items.entity.packageName}.${assign_EntityName};

/**
 * ${content.content.remark}${content.content.tableName}相关操作
 * @author ist
 */
@Slf4j
@Controller
<#list "${content.items.controller.packageName}"?split(".") as package>
<#if !package_has_next>
@RequestMapping("/${package}/${content.content.tableName}")
</#if>
</#list>
public class ${content.items.controller.className} extends BaseController{
	@Autowired
	private ${content.items.service.className} ${tableName}ServiceI;

	//状态转换
	private void setSelectOptionFromCatch(ModelMap modelMap) {
		//当前登陆人所在机构号
		modelMap.put("currorgankey",LoginManager.getAuthBean().getT00_userDTO().getOrgankey());
		//当前登陆人所在机构名
		modelMap.put("currorganname",LoginManager.getAuthBean().getT00_userDTO().getOrganname());
		//当前登陆人用户名
		modelMap.put("currusername", LoginManager.getUserName());
		//当前登陆人用户真实姓名
		modelMap.put("currrealname", LoginManager.getAuthBean().getT00_userDTO().getRealname());
	}


	/**
	* 进入列表页
	* @param ${tableName}
	* @return
	*/
	@RequestMapping(value = "/query${tableName?cap_first}ListView.do")
	public String query${tableName?cap_first}ListView(${assign_EntityName} ${tableName},ModelMap modelMap) {
		setSelectOptionFromCatch(modelMap);
	<#list "${content.items.controller.packageName}"?split(".") as package>
		<#if !package_has_next>
		return "${package}/${content.content.tableName}_list";
		</#if>
	</#list>
	}


	/**
	* 获取列表数据
	* @param ${tableName}
	* @return
	*/
	@RequestMapping(value = "/query${tableName?cap_first}List.ajax")
	@ResponseBody
	@Paging
	public PageInfo query${tableName?cap_first}List(${assign_EntityName} ${tableName},PageParam pageParam) {
		PageInfo pageInfo = null;
		try {
			MyBatisPageHelper.startPage(pageParam);
			List<${assign_EntityName}> list = ${tableName}ServiceI.get${tableName?cap_first}List(${tableName});
			DictUtil.transcoding(list);
			pageInfo = MyBatisPageHelper.getPageInfo(list);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			log.error("查询失败!", e);
		}
		return pageInfo;
	}


	<#if content.content.primaryField??>
	<#assign assign_fieldType = content.content.primaryField[0].fieldType>
	<#assign assign_fieldName = content.content.primaryField[0].fieldName>
	/**
	* 进入表单页
	* @param ${tableName}
	* @return
	*/
	@RequestMapping(value = "/query${tableName?cap_first}FormView.do")
	public String query${tableName?cap_first}FormView(${assign_EntityName} ${tableName},ModelMap modelMap,String queryFlag) {
		String formPage="";
		try {
			setSelectOptionFromCatch(modelMap);
			if("1".equals(queryFlag)){//新增
		<#list "${content.items.controller.packageName}"?split(".") as package>
			<#if !package_has_next>
				formPage="${package}/${content.content.tableName}_add";
			</#if>
		</#list>
			}else if("2".equals(queryFlag)){//修改
				${tableName} = ${tableName}ServiceI.get${tableName?cap_first}Disp(${tableName});
				DictUtil.transcoding(${tableName});
				${tableName}.setBusinesskey(${tableName}.get${content.content.primaryField[0].fieldNamePascal}());

				modelMap.put("${content.content.tableName}",${tableName});
		<#list "${content.items.controller.packageName}"?split(".") as package>
			<#if !package_has_next>
				formPage="${package}/${content.content.tableName}_modify";
			</#if>
		</#list>
			}else{//详情
		        ${tableName} = ${tableName}ServiceI.get${tableName?cap_first}Disp(${tableName});
				DictUtil.transcoding(${tableName});
				modelMap.put("${content.content.tableName}",${tableName});
		<#list "${content.items.controller.packageName}"?split(".") as package>
			<#if !package_has_next>
				formPage="${package}/${content.content.tableName}_disp";
			</#if>
		</#list>

			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			log.error("初始化失败!", e);
		}
		return formPage;
	}



	/**
	* 新增数据
	* @param ${tableName}
	* @return
	*/
	@RequestMapping(value = "/insert${tableName?cap_first}.ajax")
	@ResponseBody
	@LoggerHandlerRequired(modulename = "${content.content.tableName}表新增操作", operate_type = OperateType.ADD, operate_desc = "新增")
	public OprMessage insert${tableName?cap_first}(${assign_EntityName} ${tableName}) {
		OprMessage oprMessage = new OprMessage("1","新增成功!");
		try {
			${tableName}.set${content.content.primaryField[0].fieldNamePascal}(UUIDUtil.getUUID());
		    ${tableName}ServiceI.insert${tableName?cap_first}(${tableName});
			oprMessage.setBusinesskey(${tableName}.get${content.content.primaryField[0].fieldNamePascal}());//业务主键
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			log.error("新增失败!", e);
			oprMessage.setFlagMessage("0","新增失败!" + e.getMessage());
		}
		return oprMessage;
	}


	/**
	* 更新数据
	* @param ${tableName}
	* @return
	*/
	@RequestMapping(value = "/update${tableName?cap_first}.ajax")
	@ResponseBody
	@LoggerHandlerRequired(modulename = "${content.content.tableName}表更新操作", operate_type = OperateType.UPDATE, operate_desc = "更新")
	public OprMessage update${tableName?cap_first}(${assign_EntityName} ${tableName}) {
		OprMessage oprMessage = new OprMessage("1","更新成功!");
		try {
			${tableName}ServiceI.update${tableName?cap_first}(${tableName});
			oprMessage.setBusinesskey(${tableName}.get${content.content.primaryField[0].fieldNamePascal}());//业务主键
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			log.error("更新失败!", e);
			oprMessage.setFlagMessage("0","更新失败!" + e.getMessage());
		}
		return oprMessage;
	}


	/**
	* 删除数据
	* @param ${tableName}
	* @return
	*/
	@RequestMapping(value = "/delete${tableName?cap_first}.ajax")
	@ResponseBody
	@LoggerHandlerRequired(modulename = "${content.content.tableName}表删除操作", operate_type = OperateType.DELETE, operate_desc = "删除")
	public OprMessage delete${tableName?cap_first}(${assign_EntityName} ${tableName}) {
		OprMessage oprMessage = new OprMessage("1","删除成功!");
		try {
		${tableName}ServiceI.delete${tableName?cap_first}(${tableName});
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			log.error("删除失败!", e);
			oprMessage.setFlagMessage("0","删除失败!" + e.getMessage());
		}
		return oprMessage;
	}
	</#if>

}
